﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class StartGameUIManager : MonoBehaviour
{
    //某些游戏不需要加载进度条......
    public static string[] UnwantedSliderGame;
    //开始的游戏是什么 QuanMinLaoYu
    private string gameName;
    //获取UI上的按钮，logo图片
    private Image btnReturn, btnStart, btnHelp, logoImg, helpPanel, backgroundImg;
    //沉迷游戏提醒
    private GameObject explain;
    //跳伞的开始ui
    private GameObject tiaosan_StartUI;


    void Awake()
    {
        UnwantedSliderGame = new[] { "ShuZiTanQiu", "TianKongTanQiu", "SuJi", "SuSuan", "ShuiGuoTanQiu", "ShuZiDuoBiQiu", "DaYuChiXiaoYu", "BaoShiZhengDuo" };
        tiaosan_StartUI = GameObject.Find("BGPanel/GameStartUI/Tiaosan_StartUI");
        tiaosan_StartUI.SetActive(false);

    }
    // Use this for initialization
    void Start()
    {
        backgroundImg = GameObject.Find("BGPanel").GetComponent<Image>();
        explain = GameObject.Find("BGPanel/GameStartUI/Explain");
        btnReturn = GameObject.Find("BGPanel/GameStartUI/BtnReturn").GetComponent<Image>();
        btnStart = GameObject.Find("BGPanel/GameStartUI/BtnStart").GetComponent<Image>();
        btnHelp = GameObject.Find("BGPanel/GameStartUI/BtnHelp").GetComponent<Image>();
        logoImg = GameObject.Find("BGPanel/GameStartUI/LogoImg").GetComponent<Image>();
        helpPanel = GameObject.Find("HelpPanel").GetComponent<Image>();

    }

    private void OnInitGameHandler(string gameName)
    {
        if (OSBridgeManager.Instance.GameName == "QuanMinLaoYu" || OSBridgeManager.Instance.GameName == "GaoKongTiaoSan")
        {
            btnHelp.GetComponent<Image>().overrideSprite = Resources.Load<Sprite>("Help");
            btnHelp.transform.localPosition = new Vector3(0.3f, -155.7f, 0);
            btnHelp.GetComponent<RectTransform>().sizeDelta = new Vector2(171.6f, 28.6f);
        }
        this.gameName = gameName;
        //if ("DaDiShu" == gameName)
        //    return;

        string fullPath = gameName + "/UI/" + gameName;

        backgroundImg.overrideSprite = Resources.Load<Sprite>(fullPath + "_BackGroundImg");
        if (UnwantedSliderGame.Contains(gameName))
        {
            backgroundImg.overrideSprite = Resources.Load<Sprite>(fullPath + "_BackGroundImg");
        }
        btnReturn.overrideSprite = Resources.Load<Sprite>(fullPath + "_BtnReturn");
        btnStart.overrideSprite = Resources.Load<Sprite>(fullPath + "_BtnStart");
        logoImg.overrideSprite = Resources.Load<Sprite>(fullPath + "_Logo");
        helpPanel.overrideSprite = Resources.Load<Sprite>(fullPath + "_HelpPlane");
        btnHelp.overrideSprite = Resources.Load<Sprite>(fullPath + "_HelpImg");
        //每个场景的微调

        if (gameName== "GaoKongTiaoSan")
        {
            tiaosan_StartUI.SetActive(true);
        }
        else
        {
            tiaosan_StartUI.transform.parent.transform.Find("GaoKongTiaoSan_A").gameObject.SetActive(false);
            tiaosan_StartUI.SetActive(false);
        }

        Vector3 returnPos, logoPos, startPos, helpPos;
        Vector2 returnSize, logoSize, startSize, helpSize;
        switch (gameName)
        {
                

            case "DaDiShu":
                DonneedTurnRot();
                explain.SetActive(true);
                returnPos = new Vector3(-367, 198, 0);
                returnSize = new Vector2(68, 62);
                logoPos = new Vector3(0, 78, 0);
                logoSize = new Vector2(224, 210);
                startPos = new Vector3(-0, -85, 0);
                startSize = new Vector2(199, 50);
                helpPos = new Vector3(0, -144f, 0);
                helpSize = new Vector2(300, 50);
                LayOut(returnPos, returnSize, logoPos, logoSize, startPos, startSize, helpPos, helpSize);
                break;
            case "QuanMinLaoYu":
                DonneedTurnRot();
                explain.SetActive(true);
                returnPos = new Vector3(-367, 198, 0);
                returnSize = new Vector2(68, 62);
                logoPos = new Vector3(0, 25, 0);
                logoSize = new Vector2(277, 95);
                startPos = new Vector3(-0, -85, 0);
                startSize = new Vector2(199, 50);
                helpPos = new Vector3(0, -148f, 0);
                helpSize = new Vector2(105, 31);
                LayOut(returnPos, returnSize, logoPos, logoSize, startPos, startSize, helpPos, helpSize);
                btnHelp.overrideSprite = Resources.Load<Sprite>("Help");//不需要也行
                break;
            case "GaoKongTiaoSan":
                DonneedTurnRot();
                returnPos = new Vector3(-367, 198, 0);
                returnSize = new Vector2(68, 62);
                logoPos = new Vector3(-225, 126, 0);
                logoSize = new Vector2(319, 132);
                startPos = new Vector3(-0, -199.5f, 0);
                startSize = new Vector2(172.1f, 49.6f);
                helpPos = new Vector3(0, -143f, 0);
                helpSize = new Vector2(105, 31);
                LayOut(returnPos, returnSize, logoPos, logoSize, startPos, startSize, helpPos, helpSize);
                btnHelp.overrideSprite = Resources.Load<Sprite>("Help");
                explain.SetActive(true);
                explain.GetComponent<RectTransform>().localPosition = new Vector3(0, 210, 0);
                break;
            case "ArZhanJi":
                DonneedTurnRot();
                returnPos = new Vector3(-351, 184f, 0);
                returnSize = new Vector2(81, 74);
                logoPos = new Vector3(-0, 41f, 0);
                logoSize = new Vector2(312, 139);
                startPos = new Vector3(-0, -146, 0);
                startSize = new Vector2(172.1f, 49.6f);
                helpPos = new Vector3(0, -89f, 0);
                helpSize = new Vector2(300, 50);
                LayOut(returnPos, returnSize, logoPos, logoSize, startPos, startSize, helpPos, helpSize);
                explain.SetActive(true);
                break;
            case "ShuZiTanQiu":
                DonneedTurnRot();
                explain.SetActive(false);
                logoImg.GetComponent<RectTransform>().sizeDelta = new Vector2(325, 114);
                logoImg.GetComponent<RectTransform>().localPosition = new Vector3(0, 90f, 0);
                btnReturn.GetComponent<RectTransform>().sizeDelta = new Vector2(81, 74);
                btnReturn.GetComponent<RectTransform>().localPosition = new Vector3(-351, 184f, 0);
                btnHelp.transform.localPosition = new Vector3(0, -91, 0);
                btnHelp.GetComponent<RectTransform>().sizeDelta = new Vector2(300, 50);
                btnStart.transform.localPosition = new Vector3(0, -32, 0);
                btnStart.GetComponent<RectTransform>().sizeDelta = new Vector2(199, 50);
                break;
            case "TianKongTanQiu":
                explain.SetActive(false);
                logoImg.GetComponent<RectTransform>().sizeDelta = new Vector2(252, 100);
                logoImg.GetComponent<RectTransform>().localPosition = new Vector3(-215, 0, 0);
                logoImg.GetComponent<RectTransform>().transform.eulerAngles = new Vector3(0, 0, 90);
                btnHelp.transform.localScale = Vector3.zero;
                btnReturn.transform.localPosition = new Vector3(182, 0, 0);
                btnReturn.transform.eulerAngles = Vector3.zero;
                btnReturn.GetComponent<RectTransform>().sizeDelta = new Vector2(68, 62);
                btnStart.transform.localPosition = new Vector3(-22, 0, 0);
                btnStart.GetComponent<RectTransform>().sizeDelta = new Vector2(146, 142);
                btnStart.transform.eulerAngles = new Vector3(0, 0, 90);
                break;
            case "SuJi":
                needTurnRot();
                logoImg.GetComponent<RectTransform>().sizeDelta = new Vector2(242, 228);
                logoImg.GetComponent<RectTransform>().localPosition = new Vector3(-190, 0, 0);
                btnReturn.transform.localPosition = new Vector3(214, 0, 0);
                btnReturn.GetComponent<RectTransform>().sizeDelta = new Vector2(275, 51);
                btnStart.transform.localPosition = new Vector3(103, 0, 0);
                btnStart.GetComponent<RectTransform>().sizeDelta = new Vector2(257, 51);
                btnHelp.transform.localPosition = new Vector3(320, 0, 0);
                btnHelp.GetComponent<RectTransform>().sizeDelta = new Vector2(300, 50);

                break;
            case "ShuiGuoTanQiu":
                needTurnRot();
                logoImg.GetComponent<RectTransform>().sizeDelta = new Vector2(292, 90);
                logoImg.GetComponent<RectTransform>().localPosition = new Vector3(-239, 0, 0);
                btnReturn.transform.localPosition = new Vector3(206, -40.7f, 0);
                btnReturn.GetComponent<RectTransform>().sizeDelta = new Vector2(60, 66);
                btnStart.transform.localPosition = new Vector3(85, 0, 0);
                btnStart.GetComponent<RectTransform>().sizeDelta = new Vector2(141, 109);
                btnHelp.transform.localPosition = new Vector3(206, 41.6f, 0);
                btnHelp.GetComponent<RectTransform>().sizeDelta = new Vector2(60, 66);

                break;
            case "DaYuChiXiaoYu":
                DonneedTurnRot();
                explain.SetActive(true);
                logoImg.GetComponent<RectTransform>().localPosition = new Vector3(0, 113, 0);
                logoImg.GetComponent<RectTransform>().sizeDelta = new Vector2(363, 156);
                btnStart.transform.localPosition = new Vector3(0, -8, 0);
                btnStart.GetComponent<RectTransform>().sizeDelta = new Vector2(169, 54);
                btnReturn.transform.localPosition = new Vector3(0, -71, 0);
                btnReturn.GetComponent<RectTransform>().sizeDelta = new Vector2(169, 54);
                btnHelp.transform.localPosition = new Vector3(0, -135, 0);
                btnHelp.GetComponent<RectTransform>().sizeDelta = new Vector2(169, 54);
                break;
            case "ShuZiDuoBiQiu":
                needTurnRot();
                logoImg.GetComponent<RectTransform>().sizeDelta = new Vector2(452, 90);
                logoImg.GetComponent<RectTransform>().localPosition = new Vector3(-197, 0, 0);
                btnReturn.transform.localPosition = new Vector3(178, -40.7f, 0);
                btnReturn.GetComponent<RectTransform>().sizeDelta = new Vector2(46, 50);
                btnStart.transform.localPosition = new Vector3(63, 0, 0);
                btnStart.GetComponent<RectTransform>().sizeDelta = new Vector2(115, 115);
                btnHelp.transform.localPosition = new Vector3(178, 34.7f, 0);
                btnHelp.GetComponent<RectTransform>().sizeDelta = new Vector2(46, 50);
                break;
            case "SuSuan":
                needTurnRot();
                logoImg.GetComponent<RectTransform>().sizeDelta = new Vector2(292, 90);
                logoImg.GetComponent<RectTransform>().localPosition = new Vector3(-260.5f, 0, 0);
                btnReturn.transform.localPosition = new Vector3(75.7f, 0, 0);
                btnReturn.GetComponent<RectTransform>().sizeDelta = new Vector2(165.8f, 55.2f);
                btnStart.transform.localPosition = new Vector3(-2.6f, 0, 0);
                btnStart.GetComponent<RectTransform>().sizeDelta = new Vector2(165.8f, 55.2f);
                btnHelp.transform.localPosition = new Vector3(150.8f, 0, 0);
                btnHelp.GetComponent<RectTransform>().sizeDelta = new Vector2(165.8f, 55.2f);
                break;
            case "BaoShiZhengDuo":
                DonneedTurnRot();
                explain.SetActive(true);
                logoImg.GetComponent<RectTransform>().localPosition = new Vector3(0, 121, 0);
                logoImg.GetComponent<RectTransform>().sizeDelta = new Vector2(314, 136);
                btnStart.transform.localPosition = new Vector3(0, -8, 0);
                btnStart.GetComponent<RectTransform>().sizeDelta = new Vector2(176, 57);
                btnReturn.transform.localPosition = new Vector3(0, -75, 0);
                btnReturn.GetComponent<RectTransform>().sizeDelta = new Vector2(176, 57);
                btnHelp.transform.localPosition = new Vector3(0, -141, 0);
                btnHelp.GetComponent<RectTransform>().sizeDelta = new Vector2(176, 57);
                break;
        }
        
    }
    //不需要旋转90
    void DonneedTurnRot()
    {
        logoImg.transform.eulerAngles = new Vector3(0,0,0);
        btnReturn.transform.eulerAngles = new Vector3(0, 0, 0);
        btnStart.transform.eulerAngles = new Vector3(0, 0, 0);
        btnHelp.transform.eulerAngles = new Vector3(0, 0, 0);
    }

    void needTurnRot()
    {
        explain.SetActive(false);
        logoImg.GetComponent<RectTransform>().transform.eulerAngles = new Vector3(0, 0, 90);
        btnReturn.transform.eulerAngles = new Vector3(0, 0, 90);
        btnStart.transform.eulerAngles = new Vector3(0, 0, 90);
        btnHelp.transform.eulerAngles = new Vector3(0, 0, 90);
    }

    void LayOut(Vector3 ReturnlocalPosition,Vector2 ReturnlocalSize, Vector3 LogolocalPosition,Vector2 LogoSize,Vector3 StartlocalPosition,Vector2 StartSzie, Vector3 HelplocalPosition, Vector2 HelpSzie)
    {
        btnReturn.transform.localPosition = ReturnlocalPosition;
        btnReturn.GetComponent<RectTransform>().sizeDelta = ReturnlocalSize;
        
        logoImg.GetComponent<RectTransform>().localPosition = LogolocalPosition;
        logoImg.GetComponent<RectTransform>().sizeDelta = LogoSize;

       
        btnStart.GetComponent<RectTransform>().localPosition = StartlocalPosition;
        btnStart.GetComponent<RectTransform>().sizeDelta = StartSzie;
        
        btnHelp.gameObject.transform.localPosition =HelplocalPosition;
        btnHelp.GetComponent<RectTransform>().sizeDelta = HelpSzie;
    }

    private void OnEnable()
    {
        EventDispatcher.AddEvent<string>("OnInitGame", OnInitGameHandler);
    }

    private void OnDisable()
    {
        EventDispatcher.RemoveEvent<string>("OnInitGame", OnInitGameHandler);
    }


}
